package com.gzsxy.excel.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.fastjson.JSON;
import com.gzsxy.excel.constants.sysUserConstants;
import com.gzsxy.excel.entity.DemandTo;
import com.gzsxy.excel.entity.ProductTo;
import com.gzsxy.excel.entity.SysUserTo;
import com.gzsxy.excel.entity.TeamUserTo;
import com.gzsxy.excel.log.annotation.OperateLog;
import com.gzsxy.excel.util.ExcelUtils;
import com.gzsxy.excel.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName
 * @Description TODO
 * @Author
 * @Date 22/1/7
 * @Version 1.0
 **/

@RestController
@RequestMapping(value = "export")
@Api(value = "Excel模板导出信息", tags = "Excel模板导出信息")
@Slf4j
public class ExportController {


    @ApiOperation(value = "用户模板导出")
    @OperateLog("用户模板导出")
    @RequestMapping(value = "sysUserET")
    public static void exportSysUserExcelTitle(HttpServletResponse response) throws Exception {
        ExcelUtils.downloadUnfilledToXlsx("新增用户模板",response, SysUserTo.class,null,null);
    }


    @ApiOperation(value = "团队成员模板导出")
    @OperateLog("团队成员模板导出")
    @RequestMapping(value = "teamUserET")
    public static void exportProductTeamUserExcelTitle(HttpServletResponse response) throws Exception {
        ExcelUtils.downloadUnfilledToXlsx("新增团队成员模板",response, TeamUserTo.class,null,null);
    }

    @ApiOperation(value = "新增需求模板导出")
    @OperateLog("新增需求模板导出")
    @RequestMapping(value = "demandET")
    public static void exportProductDemandExcelTitle(HttpServletResponse response) throws Exception {
        ExcelUtils.downloadUnfilledToXlsx("新增产品需求模板",response, DemandTo.class,null,null);
    }

    @ApiOperation(value = "新增产品模板导出")
    @OperateLog("新增产品模板导出")
    @RequestMapping(value = "productET")
    public static void exportProductExcelTitle(HttpServletResponse response) throws Exception {
        ExcelUtils.downloadUnfilledToXlsx("新增产品模板",response, ProductTo.class,null,null);
    }



    @PostMapping(value = "/upload")
    @ApiOperation("导入用户")
    public R<?> upload(@RequestParam("file") MultipartFile file) {
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        // 这里每次会读取3000条数据 然后返回过来 直接调用使用数据就行
        try {
            EasyExcel.read(file.getInputStream(), SysUserTo.class, new PageReadListener<SysUserTo>(dataList -> {
                for (SysUserTo demoData : dataList) {
                    log.info("获取到记录【{}】",JSON.toJSONString(demoData));
                    demoData.setEducation(sysUserConstants.educationEu.getNameByValue(demoData.getEducation()));
                }
            })).sheet().doRead();
        } catch (IOException e) {
//            new ServiceException("Excel导入失败");
            return R.failed("Excel导入失败");
        }
        return R.ok("Excel导入成功");
    }


    public static void main(String[] args) {
        String str ="{\"dataKey\":\"{\\\"BZ\\\":\\\"\\\",\\\"DCSJ\\\":\\\"2022-03-11 13:55:15\\\",\\\"DHSJ\\\":\\\"2022-03-11 14:31:00\\\",\\\"DJSJ\\\":\\\"2022-03-11 13:48:01\\\",\\\"DWDM\\\":\\\"440300111\\\",\\\"FLYZ\\\":\\\"否\\\",\\\"HJDD\\\":\\\"440100113\\\",\\\"HJFS\\\":\\\"4\\\",\\\"HJLX\\\":\\\"4\\\",\\\"HJPZJG\\\":\\\"440300111\\\",\\\"HJSM\\\":\\\"\\\",\\\"HJYXQ\\\":\\\"2022-03-11\\\",\\\"JBR\\\":\\\"曾炳之\\\",\\\"JBSJ\\\":\\\"2022-03-11 13:48:01\\\",\\\"JCJG\\\":\\\"\\\",\\\"JCR\\\":\\\"律师会见系统\\\",\\\"JCSJ\\\":\\\"2022-03-11 14:31:00\\\",\\\"JGBZ\\\":\\\"\\\",\\\"JQMJ\\\":\\\"律师会见系统\\\",\\\"JSSJ\\\":\\\"2022-03-11 14:31:00\\\",\\\"JSXBH\\\":\\\"\\\",\\\"KSSJ\\\":\\\"2022-03-11 14:03:24\\\",\\\"LDRQ\\\":\\\"\\\",\\\"LDXM\\\":\\\"\\\",\\\"LDYJ\\\":\\\"\\\",\\\"LSDW\\\":\\\"泰和泰(深圳)律师事务所\\\",\\\"LSDW2\\\":\\\"\\\",\\\"LSXM\\\":\\\"黄晓霞\\\",\\\"LSXM2\\\":\\\"\\\",\\\"LSZHM\\\":\\\"14403201011202565\\\",\\\"LSZHM2\\\":\\\"\\\",\\\"LXFSDH\\\":\\\"\\\",\\\"LXFSDH2\\\":\\\"\\\",\\\"LXFSSJ\\\":\\\"13048812292\\\",\\\"QTR\\\":\\\"\\\",\\\"RS\\\":\\\"\\\",\\\"RYBH\\\":\\\"440300111202108292890\\\",\\\"WTJD\\\":\\\"一审\\\",\\\"WTRXM\\\":\\\"李磊\\\",\\\"WTSLX\\\":\\\"\\\",\\\"XKJDWSH\\\":\\\"\\\",\\\"ZBMJ\\\":\\\"曾炳之\\\",\\\"ZJHM\\\":\\\"330222198104191529\\\",\\\"ZJHM2\\\":\\\"\\\",\\\"ZLLSSWS\\\":\\\"\\\",\\\"ZLLSXM\\\":\\\"\\\",\\\"ZLZJH\\\":\\\"\\\",\\\"ZYZM\\\":\\\"\\\"}\",\"id\":\"L_Y_62148319\",\"type\":\"appoint\"}";

        String s = JSON.toJSONString(str);
        System.out.println(s);
    }
}


